<template>
  <div class="login">


    <el-form :model="loginForm" status-icon :rules="rules" ref="loginForm" label-width="100px" class="demo-ruleForm">
      <h1 class="title">
        <i class="el-icon-setting"></i>
        vuexms登录
      </h1>

      <el-form-item label="账号" prop="username">
        <el-input type="text" v-model="loginForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
        <el-button @click="resetForm('loginForm')">清空</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
  import {mapGetters,mapMutations,mapActions} from 'vuex'

  const loginSuccess = {
    message: '登录成功',
    type: 'success'
  }

  export default {

    name: 'login',
    data() {

      return {
        // 登录表单的数据
        loginForm: {
          username: '',
          password: ''

        },
        // 验证规则
        rules: {
          username: [
            {required: true, message: '请输入用户名', trigger: 'blur'}, //非空验证
            {min: 5, max: 18, message: '长度在 5 到 18 个字符', trigger: 'blur'} //长度验证
          ],
          password: [
            {required: true, message: '请输入密码', trigger: 'blur'},
            {min: 5, max: 18, message: '长度在 5 到 18 个字符', trigger: 'blur'}
          ]

        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // 保留this
            let _this = this
            // 收集用户名和密码 发送给后端
            this.axios.post('/api/checklogin', {
              username: _this.loginForm.username,
              password: _this.loginForm.password

            }).then(response => {
             let userInfo= response.data[0];
             // 把用户信息存入本地存储
              this.storage.set('userInfo',userInfo)
              console.log('接收后端响应登录请求的数据: ', userInfo)
              if (userInfo) {
                _this.$message(loginSuccess)
                _this.setUserInfo(userInfo)
                // 跳转到后台首页
                _this.$router.push('/')
              } else {
                _this.$message.error('登录失败')
              }
            })


          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      ...mapMutations({
        setUserInfo:'SET_USER_INFO'
      })
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
  html, body {
    margin: 0;
    padding: 0;
  }

  html, body, #app, .login {
    height: 100%;
  }

  .login {
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .el-form {
    width: 400px;
    border: 1px solid #ccc;
    border-radius: 4px;
    padding: 0px 45px 10px 10px;
  }

  .el-form .title {
    font-size: 22px;
    color: #333;
    font-weight: bold;
    padding: 20px 0 20px 60px;

  }


</style>
